package com.leyou.item.controller;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

//    GET /category/of/parent?pid=0

    @Autowired
    private CategoryService categoryService;

        @GetMapping("/of/parent")
//        声明可以有哪些网站进入到此方法 manage.leyou.com
//        @CrossOrigin(origins="http://manage.leyou.com")  //跨域资源共享
        public ResponseEntity<List<CategoryDTO>> findCateogryByParentId(
                @RequestParam(value = "pid",defaultValue = "0") Long pid){
            List<CategoryDTO> categoryDTOList = categoryService.findCateogryByParentId(pid);
            return ResponseEntity.ok(categoryDTOList);
//            return ResponseEntity.status(203).body(categoryDTOList);
        }

    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> findCateogryByBrandId(
            @RequestParam(value = "id") Long id){
        List<CategoryDTO> categoryDTOList = categoryService.findCateogryByBrandId(id);
        return ResponseEntity.ok(categoryDTOList);
//            return ResponseEntity.status(203).body(categoryDTOList);
    }

    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> findCateogryByCids(
            @RequestParam(value = "ids") List<Long> ids){
        List<CategoryDTO> categoryDTOList = categoryService.findCateogryByCids(ids);
        return ResponseEntity.ok(categoryDTOList);
//            return ResponseEntity.status(203).body(categoryDTOList);
    }
}
